const router = require('express').Router()
const { Movie, ScreeningTimes, ScreeningRoom, Seat } = require('@/models')
const { data } = require('@/utils/send')

router.get('/', async (req, res) => {
    try {
        const { id } = req.query
        const [movie, screeningTimes] = await Promise.all([
            Movie.findOne({ where: { id } }),
            ScreeningTimes.findAll({ where: { movie_id: id, is_delete: 0 } })
        ])
        const roomId = screeningTimes.map(e => e.auditorium_id)
        const romms = await Promise.all(roomId.map(async (id) => await GetRoom(id)))
        screeningTimes.forEach((e, index) => {
            e.dataValues.room = romms[index]
        });
        const datas = data({ message: '获取成功', data: { movie, screeningTimes } })
        res.send(datas)
        res.locals.data = datas
    } catch (error) {
        res.cc(error)
    }
})
async function GetRoom(id) {
    const room = await ScreeningRoom.findOne({ where: { id } })
    return room
}
router.post('/comment', async (req, res) => {
    try {
        const { comment } = req.body
        if (!comment) res.cc('请输入评论')
        res.send(data({ message: '评论成功' }))
        res.locals.data = data({ message: '评论成功' })
    } catch (error) {
        res.cc(error)
    }
})
router.get('/seats', async (req, res) => {
    try {
        const { id } = req.query
        const seat = await Seat.findOne({ where: { time_id: id } })
        if (!seat) return res.cc('没有座位信息')
        const datas = data({ message: '获取成功', data: seat })
        res.send(datas)
        res.locals.data = datas
    } catch (error) {
        res.cc(error)
    }
})
module.exports = router